package yf.mall.utils.app;

import com.yf.utils.DateUtil;
import org.apache.commons.lang3.time.DateUtils;
import yf.mall.common.Const;
import yf.mall.enums.app.AppDateTypeEnum;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author yxmds
 */
public final class AppUtil {

    private AppUtil() {
    }

    /**
     * 根据时间类型获取区间开始时间
     *
     * @param type
     * @return
     */
    public static Date getBeginByType(Integer type) {
        Date now = new Date();
        AppDateTypeEnum typeEnum = AppDateTypeEnum.getByKey(type);
        if (typeEnum == null) {
            return now;
        }
        switch (typeEnum) {
            case TOW_HOURS:
                return DateUtils.addHours(now, -2);
            case HOURS_24:
                return DateUtils.addHours(now, -24);
            case DAY_3: //近三日 就是两天前
                return DateUtils.addDays(now, -2);
            case DAY_7: //近七日 就是6天前
                return DateUtils.addDays(now, -6);
            case DAY_15: //近15日 就是14天前
                return DateUtils.addDays(now, -14);
            case DAY_30: //近三十日 就是29天钱
                return DateUtils.addDays(now, -29);
            case YES:
                return DateUtils.addDays(DateUtil.getStartTimeToDate(now), -1);
            case TODAY:
                return DateUtil.getStartTimeToDate(now);
            case CURR_MONTH:
                return DateUtil.firstDayOfMonth(now.getYear() + 1900, now.getMonth());
            case LAST_MONTH:
                return DateUtil.firstDayOfMonth(now.getYear() + 1900, now.getMonth() - 1);
            default:
                return now;
        }
    }

    /**
     * 根据时间类型获取区间结束时间
     *
     * @param type
     * @return
     */
    public static Date getEndByType(Integer type) {
        Date now = new Date();
        AppDateTypeEnum typeEnum = AppDateTypeEnum.getByKey(type);
        if (typeEnum == null) {
            return DateUtil.getEndTimeToDate(now);
        }
        switch (typeEnum) {
            case YES:
                return DateUtils.addDays(DateUtil.getEndTimeToDate(now), -1);
            case CURR_MONTH:
                return DateUtil.lastDayOfMonth(now.getYear() + 1900, now.getMonth());
            case LAST_MONTH:
                return DateUtil.lastDayOfMonth(now.getYear() + 1900, now.getMonth() - 1);
            default:
                return DateUtil.getEndTimeToDate(now);
        }
    }

    public static Integer getIntegerDate(Date begin) {
        if (begin == null) {
            begin = new Date();
        }
        String dateStr = DateUtil.dateFormat("yyyyMMdd", begin);
        return Integer.parseInt(dateStr);
    }

    public static String getBeforeTimeStr(Integer beforeHour) {
        Date date = DateUtils.addHours(new Date(), -1 * beforeHour);
        return DateUtil.dateFormat("HH:mm", date);
    }

    public static String getNumberStr(BigDecimal amount) {
        if (amount == null) {
            return "";
        }
        BigDecimal realAmount = amount.divide(Const.HUNDRED, 1, RoundingMode.HALF_DOWN);
        return getNumberStrToWan(realAmount, 1);
    }

    public static String getNumberStrToWan(BigDecimal number, int scale) {
        if (number.compareTo(Const.WAN) > 0) {
            BigDecimal result = number.divide(Const.WAN, scale, BigDecimal.ROUND_HALF_UP);
            DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
            return decimalFormat.format(result) + "万";
        }
        return number.toPlainString();
    }


    public static String convertScoreLv(String scoreRating) {
        return Objects.equals("1", scoreRating) ? "高" : "";
    }

    public static String calRatio(Integer dividend, Integer divisor) {
        BigDecimal quotient = new BigDecimal(dividend).multiply(new BigDecimal(100).divide(new BigDecimal(divisor), 1, RoundingMode.HALF_DOWN));
        return quotient.toPlainString();
    }

    public static void main(String[] args) {
        System.out.println(getBeginByType(6));
    }
}
